<template>
  <div class="wrapper">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{path:'/5-1'}">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:'/5-1'}">账号管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增账号</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form label-width="100px" :model="ruleForm" :rules="rules" ref="ruleForm" class="mainform">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="登录名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="账号角色" prop="actor">
        <el-select v-model="ruleForm.actor" placeholder="请选择角色">
          <el-option v-for="item in options" :label="item.label" :value="item.value" :key="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password" type="password" v-show="!ruleForm.showcode"><i slot="suffix" class="iconfont" @click="handleicon">&#xe618;</i></el-input>
        <el-input v-model="ruleForm.password" v-show="ruleForm.showcode"><i slot="suffix" class="iconfont" @click="handleicon">&#xe63b;</i></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="goback">返回</el-button>
        <el-button type="warning" @click="submitForm('ruleForm')">提交</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
  import axios from 'axios'
  import {newaccountAdd} from '@/api/accountmanagement/account'
  import {rolelist} from '@/api/role/role'
  export default {
    name: "NewAccount",
    data() {
      return {
        ruleForm: {
          name: '',
          login: '',
          role: '',
          pwd: '',
          showcode:false,
        },
        options: [],
        rules: {
          name: [
            {required: true, message: '请输入姓名', trigger: 'blur'},
            {min: 1, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'},
            {pattern: /^[\u4e00-\u9fa5]{0,}$/, message: '请输入中文姓名', trigger: 'blur'},
          ],
          username: [
            {required: true, message: '请输入登录名', trigger: 'blur'},
            {min: 3, max: 100, message: '登录名过短比较危险哦', trigger: 'blur'},
          ],
          actor: [
            { required: true, message: '请至少选择一个角色', trigger: 'change'}
          ],
          password: [
            {required: true, message: '请输入密码', trigger: 'blur'},
          ]
        }
      }
    },
    mounted(){
       this.getDetailInfo();
    },
    methods: {
      handleicon(){
        this.ruleForm.showcode=!this.ruleForm.showcode;
      },
      getDetailInfo() {
        let params={};
        params.page=1;
        params.size=10;
        rolelist(params).then(this.handleGetDataSucc)
      },
      handleGetDataSucc(res) {
        console.log(res);
        let rolename=res.data.UserList;
        for (let item of rolename) {
          this.options.push(
            {
              label: item.actor,
              value: item.actor
            }
          )
        }
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$confirm('确定创建后台账号吗?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'success',
              center: true
            }).then(() => {
              this.$message({
                type: 'success',
                message: '创建成功!'
              });
              let params={};
              params.createBy=JSON.parse(localStorage.userinfo).id;
              params.username=this.ruleForm.username;
              params.name=this.ruleForm.name;
              params.actor=this.ruleForm.actor;
              console.log(params.competence);
              params.password=parseInt(this.ruleForm.password);
              params.types=2;
              var _this=this;
              newaccountAdd(params).then(function (res) {
                if(res.status===200){
                  _this.$router.push({path:'/5-1'});
                  console.log(res);
                }
              })
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消创建'
              });
            })
          } else {
            return false;
          }
        });
      },
      goback(){
        this.$router.push({name:'AccountManagement'})
      }
    },
  }
</script>

<style lang="less" scoped>
  @import "~styles/z.less";
  @import "~styles/css3.less";
  @import "~styles/variables.less";

  .mainform{
  margin-top: 40px;
  }
</style>
